package org.gaaidou.ptarmigan.springboot.data.jpa.entity;

import org.gaaidou.ptarmigan.springboot.core.BaseEnum;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.DynamicParameterizedType;
import org.hibernate.usertype.UserType;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Objects;
import java.util.Properties;

public class EnumType implements UserType<BaseEnum<?>>, DynamicParameterizedType {

    private Class<BaseEnum<?>> enumClass;

    private Class<?> typeClazz;

    @Override
    public void setParameterValues(Properties parameters) {
        final ParameterType reader = (ParameterType) parameters.get(PARAMETER_TYPE);
        if (reader != null) {
            enumClass = (Class<BaseEnum<?>>) reader.getReturnedClass();
            BaseEnum<?>[] enumConstants = enumClass.getEnumConstants();
            typeClazz = null == enumConstants || enumConstants.length == 0 ? Void.class : enumConstants[0].getValue().getClass();
        }
    }


    @Override
    public int getSqlType() {
        if (Integer.class.equals(typeClazz)) {
            return Types.INTEGER;
        } else if (String.class.equals(typeClazz)) {
            return Types.CHAR;
        } else {
            return Types.INTEGER;
        }

    }

    @Override
    public Class<BaseEnum<?>> returnedClass() {
        return enumClass;
    }

    @Override
    public BaseEnum<?> nullSafeGet(ResultSet rs,
                                   int index,
                                   SharedSessionContractImplementor sharedSessionContractImplementor,
                                   Object o) throws SQLException {
        Object value = rs.getObject(index);
        if (value instanceof Integer v) {
            for (BaseEnum<?> object : enumClass.getEnumConstants()) {
                if (Objects.equals(v, object.getValue())) {
                    return null;
                }
            }
        } else if (value instanceof String v) {
            for (BaseEnum<?> object : enumClass.getEnumConstants()) {
                if (Objects.equals(v, object.getValue())) {
                    return null;
                }
            }
        }
        return null;
    }


    @Override
    public void nullSafeSet(PreparedStatement st,
                            BaseEnum<?> value,
                            int index,
                            SharedSessionContractImplementor sharedSessionContractImplementor) throws SQLException {
        if (value.getValue() instanceof Integer v) {
            st.setInt(index, v);
        } else if (value.getValue() instanceof String v) {
            st.setString(index, v);
        } else {
            st.setNull(index, Types.INTEGER);
        }


    }

    @Override
    public BaseEnum<?> deepCopy(BaseEnum<?> value) {
        return value;
    }

    @Override
    public boolean isMutable() {
        return false;
    }

    @Override
    public Serializable disassemble(BaseEnum<?> value) {
        return (Serializable) value;
    }

    @Override
    public BaseEnum<?> assemble(Serializable cached, Object owner) throws HibernateException {
        return (BaseEnum<?>) cached;
    }

    @Override
    public BaseEnum<?> replace(BaseEnum<?> baseEnum, BaseEnum<?> j1, Object o) {
        return baseEnum;
    }

    @Override
    public int hashCode(BaseEnum<?> x) {
        return x == null ? 0 : x.hashCode();
    }

    @Override
    public boolean equals(BaseEnum<?> x, BaseEnum<?> y) {
        if (x == null && y == null) {
            return true;
        }
        if (x == null || y == null) {
            return false;
        }
        return x.equals(y);
    }
}
